<%@ page import="com.xinjie.www.service.post.PostServiceImpl" %>
<%@ page import="com.xinjie.www.po.Post" %>
<%@ page import="java.util.List" %>
<%@ page import="com.xinjie.www.service.collect.CollectServiceImpl" %>
<%@ page import="com.xinjie.www.util.Constants" %>
<%@ page import="com.xinjie.www.po.User" %>
<%@ page import="com.xinjie.www.po.Collect" %>
<%@ page import="com.xinjie.www.service.thumb.ThumbServiceImpl" %>
<%@ page import="com.xinjie.www.po.Thumb" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>浏览帖子</title>
    <style type="text/css">
        table {
            width: 100%;
            border: 1px solid red;
            border-collapse: collapse;
        }

        th, td {
            border: 1px solid red;
        }
    </style>
</head>
<body>
<%
    Object o = request.getSession().getAttribute(Constants.USER_SESSION);
    User user = (User) o;
    PostServiceImpl postService = new PostServiceImpl();
    String s = request.getParameter("tieba");
    List<Post> posts = null;
    try {
        posts = postService.getPostList(s);
    } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
    }
    CollectServiceImpl collectService = new CollectServiceImpl();
    List<Collect> collects = null;
    try {
        collects = collectService.getCollect(user.getUserId());
    } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
    }
    ThumbServiceImpl thumbService = new ThumbServiceImpl();
    List<Thumb> thumbs = null;
    try {
        thumbs = thumbService.getThumb(user.getUserId());
    } catch (SQLException | ClassNotFoundException e) {
        e.printStackTrace();
    }
%>
<div align="center">
    <h4>欢迎进入${param.tieba}</h4>
    <table>
        <tr>
            <th>贴子id</th>
            <th>主题</th>
            <th>所属贴吧</th>
            <th>内容</th>
            <th>作者</th>
            <th>评论数</th>
            <th>点赞数</th>
            <th>发布时间</th>
            <th>评论</th>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
        </tr>
        <%
            for (Post post : posts) {
        %>
        <tr>
            <%= "<td>" + post.getPostId() + "</td>"%>
            <%="<td>" + post.getTitle() + "</td>"%>
            <%="<td>" + post.getTieba() + "</td>"%>
            <%="<td>" + post.getContent() + "</td>"%>
            <%="<td>" + post.getWriter() + "</td>"%>
            <%="<td>" + post.getComment() + "</td>"%>
            <%="<td>" + post.getThumb() + "</td>"%>
            <%="<td>" + post.getTime() + "</td>"%>
            <%String id = post.getPostId() + "";%>
            <td><a href="CommentList.jsp?post=<%=id%>&tieba=<%=s%>">查看</a></td>
            <td><a href="WriteComment.jsp?postId=<%=id%>&tieba=<%=s%>">写评论</a></td>
            <%
                boolean flag1 = false;
                for (Thumb thumb : thumbs) {
                    if (thumb.getPostId() == post.getPostId()) {
                        flag1 = true;
                        break;
                    }
                }
                if (flag1) {%>
            <td><a href="ThumbList.jsp">前往取消点赞</a></td>
            <%
            } else {%>
            <td><a href="DoThumb.jsp?postId=<%=id%>&tieba=<%=s%>">点赞</a></td>
            <% }
            %>

            <%
                boolean flag2 = false;
                for (Collect collect : collects) {
                    if (collect.getPostId() == post.getPostId()) {
                        flag2 = true;
                        break;
                    }
                }
                if (flag2) {%>
            <td><a href="CollectList.jsp">前往取消收藏</a></td>
            <%} else {%>
            <td><a href="DoCollect.jsp?postId=<%=id%>&tieba=<%=s%>">收藏</a></td>
            <%}%>
            <td><a href="Doreport.jsp?postId=<%=id%>&tieba=<%=s%>">举报</a> </td>
        </tr>
        <%}%>
    </table>
    <%if (request.getSession().getAttribute("commentSuccess") != null) {%>
    <%=request.getSession().getAttribute("commentSuccess")%>
    <%request.getSession().removeAttribute("commentSuccess");%>
    <%}if (request.getSession().getAttribute("commentLoses") != null) {%>
    <%=request.getSession().getAttribute("commentLoses")%>
    <%request.getSession().removeAttribute("commentLoses");%>
    <%}if (request.getSession().getAttribute("BanComment") != null) {%>
    <%=request.getSession().getAttribute("BanComment")%>
    <%request.getSession().removeAttribute("BanComment");%>
    <%}if (request.getSession().getAttribute("CollectSuccess") != null) {%>
    <%=request.getSession().getAttribute("CollectSuccess")%>
    <%}if (request.getSession().getAttribute("CollectLose") != null) {%>
    <%=request.getSession().getAttribute("CollectLose")%>
    <%}if (request.getSession().getAttribute("doReportSuccess") != null) {%>
    <%=request.getSession().getAttribute("doReportSuccess")%>
    <%}if (request.getSession().getAttribute("doReportLose") != null) {%>
    <%=request.getSession().getAttribute("doReportLose")%>
    <%}%>
    <a href="LookTiebaList.jsp">返回</a>
</div>
</body>
</html>
